package com.source.lightfowl.person.service.impl;

import com.source.lightfowl.constant.ImageCatalogue;
import com.source.lightfowl.handler.MyException;
import com.source.lightfowl.person.dao.RoleDao;
import com.source.lightfowl.person.dao.SysUserDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.person.entity.Person_Role;
import com.source.lightfowl.person.entity.Role;
import com.source.lightfowl.person.service.UserService;
import com.source.lightfowl.utils.ImageUtil;
import com.source.lightfowl.utils.QRCodeUtil;
import com.source.lightfowl.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public void addUser(Person person, MultipartFile file) {
        String id = person.getId();
        Person user = sysUserDao.findUserById(id);

        if (file != null){
            String s = null;
            String decode = null;
            try {//解析二维码
                decode = QRCodeUtil.decode(ImageUtil.multipartFileToFile(file));
            } catch (Exception e) {
                throw new MyException(StatusCode.ANALYSE_IMG_ERROR, "二维码图片解析失败");
            }
            person.setWechatCodeDecode(decode);
            try {
                s = ImageUtil.saveImage(file, ImageCatalogue.User_Catalogue);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //删除原来的图片
            if (user != null) {
                String mag = user.getWechatCode();
                if(StringUtils.isEmpty(mag)){
                    //新增 不用处理
                }{
                    //修改 则删除原来的图片
                    ImageUtil.delete(mag,ImageCatalogue.User_Catalogue);
                }
            }
            person.setWechatCode(s);
        } else {//沒有修改图片

        }
        sysUserDao.save(person);
    }

    @Override
    public void allocateUser(String role, String id) {
        sysUserDao.setCharactFor(role, id);
    }

    @Override
    public void deleteUser(String id) {
        sysUserDao.deleteByPerson_id(id);
    }

    @Override
    public List<Person_Role> findUserAll() {
        List<Person_Role> list = new ArrayList<>();
        List<Person> plist = sysUserDao.findAll();
        Person_Role person_role = null;
        if (plist.size() != 0){
            for (Person a : plist){
                Role role = roleDao.findByRid(a.getRole());
                if (role == null) {
                    person_role = new Person_Role(a.getId(), a.getLoginName(), a.getEmail(), a.getName(),
                            a.getPhone(), a.getRole(), a.getCreateDate());
                }else{
                    person_role = new Person_Role(a.getId(), a.getLoginName(), a.getEmail(), a.getName(),
                            a.getPhone(), a.getRole(), role.getRname(), a.getCreateDate());
                }

                list.add(person_role);
            }

        }

        return list;
    }

    @Override
    public List<Person> findUserByLoginName(String loginName) {
        return sysUserDao.findByNameLike("%"+loginName+"%");
    }

    @Override
    public Person findUser(String loginName) {
        return sysUserDao.findByUsername(loginName);
    }

    @Override
    public Person login(String username, String password, String key) {
        return sysUserDao.findByUsernameAndPassword(username,password,key);
    }

    @Override
    public Person checkUser(String username, String password, String key) {
        return sysUserDao.findByUsernameAndPassword(username,password,key);
    }

    @Override
    public Person findUserById(String id) {
        return sysUserDao.findUserById(id);
    }

    @Override
    public void deleteUserRole(String id){
        sysUserDao.deleteUserRole(id);
    }

    @Override
    public List<Map<String, String>> checkBp(String id){
        return sysUserDao.findBpById(id);
    }

    @Override
    public List<Map<String, String>> checkSplate(String id, String bpid) {
        return sysUserDao.findSp(id, bpid);
    }


}
